package cn.fourhundredandeighteen.utill;

import cn.fourhundredandeighteen.UserInfo;
import cn.fourhundredandeighteen.controller.CourseController;
import cn.fourhundredandeighteen.controller.TeacherController;
import cn.fourhundredandeighteen.entity.*;
import cn.fourhundredandeighteen.view.StudentView.StudentCourseInfo.studentCourseInfo;
import cn.fourhundredandeighteen.view.StudentView.StudentFunction.StudentScore;
import cn.fourhundredandeighteen.view.TeacherView.SetStudentCourseScore.MyButtonEditor;
import cn.fourhundredandeighteen.view.TeacherView.SetStudentCourseScore.MyButtonRender;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @description:显示小工具
 * @author: liuyongqi
 * @time: 2020/12/18
 */
public class ViewUtill {
    /**
     * 封装的课程信息显示表
     *
     * @param data
     * @return
     */
    public static JScrollPane viewCourseTable(List<Course> data) {
        String[] name = {"课程号", "课程名称", "课时", "学分", "教师", "职称", "人数", "修改时间"};
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (data == null) {
            Object[][] tableData = new Object[0][8];
            return jScrollPane(tableData, name);
        }
        Object[][] tableData = new Object[data.size()][8];
        for (int i = 0; i < data.size(); i++) {
            tableData[i][0] = data.get(i).getCourseId();
            tableData[i][1] = data.get(i).getCourseName();
            tableData[i][2] = data.get(i).getCourseHour();
            tableData[i][3] = data.get(i).getCourseScore();
            //获取老师名称
            Long teacherId = data.get(i).getTeacherId();
            TeacherController teacherController = new TeacherController();
            Result<Teacher> teacherResult = teacherController.teacherInfo(teacherId);
            tableData[i][4] = teacherResult.getData().getTeacherName();
            tableData[i][5] = teacherResult.getData().getTeacherTitle();
            tableData[i][6] = data.get(i).getCourseCount();
            tableData[i][7] = dateFormat.format(data.get(i).getUpdateTime());
        }
        return jScrollPane(tableData, name);
    }

    /**
     * 封装的成绩信息显示表
     *
     * @param
     * @return
     */
    public static JScrollPane viewCourseScoreTable(Long StudentSession, Long courseid) {
        String[] name = {"课程号", "课程名称", "课时", "学分", "教师", "职称", "人数", "分数"};

        StudentScore studentScore = new StudentScore();
        Result<Score> result = studentScore.StudentScore(courseid, StudentSession);
        if (result.getMassage() == "SUCCESS") {
            CourseController courseController = new CourseController();
            Result<Course> courseId = courseController.getCourseId(courseid);
            Object[][] tableData = new Object[1][8];

            tableData[0][0] = courseId.getData().getCourseId();
            tableData[0][1] = courseId.getData().getCourseName();
            tableData[0][2] = courseId.getData().getCourseHour();
            tableData[0][3] = courseId.getData().getCourseScore();
            //获取老师名称
            Long teacherId = courseId.getData().getTeacherId();
            TeacherController teacherController = new TeacherController();
            Result<Teacher> teacherResult = teacherController.teacherInfo(teacherId);
            tableData[0][4] = teacherResult.getData().getTeacherName();
            tableData[0][5] = teacherResult.getData().getTeacherTitle();
            tableData[0][6] = courseId.getData().getCourseCount();
            tableData[0][7] = result.getData().getScore();


            return jScrollPane(tableData, name);

        }


        Object[][] tableData = new Object[0][8];
        return jScrollPane(tableData, name);

//
//
//       return jScrollPane(tableData,name);
    }

    /**
     * 学生成绩信息设置
     *
     * @param
     * @return
     */
    public static JScrollPane CourseScoreSetTable(Long text) { //课程分数设置表
        String[] name = {"学号", "姓名", "班级", "专业", "教师", "性别", "分数", " "};
        TeacherController teacherController1 = new TeacherController();
        Result<List<Student>> data = teacherController1.getCourseStudents(text);
        System.out.println(data);
        if (data.getData() == null) {
            Object[][] tableData = new Object[0][8];
            return studentSCorePane(tableData, name, text);
        }
        Object[][] tableData = new Object[data.getData().size()][8];
        for (int i = 0; i < data.getData().size(); i++) {
            tableData[i][0] = data.getData().get(i).getStudentId();
            tableData[i][1] = data.getData().get(i).getStudentName();
            tableData[i][2] = data.getData().get(i).getStudentClass();
            tableData[i][3] = data.getData().get(i).getStudentMajor();
            //获取老师名称
            Long teacherId = UserInfo.Session;
            TeacherController teacherController = new TeacherController();
            Result<Teacher> teacherResult = teacherController.teacherInfo(teacherId);
            tableData[i][4] = teacherResult.getData().getTeacherName();
            tableData[i][5] = data.getData().get(i).getStudentGender();

            StudentScore studentScore = new StudentScore();
            try {

                Result<Score> reult = studentScore.StudentScore(text, Long.valueOf(data.getData().get(i).getStudentId()));
                tableData[i][6] = reult.getData().getScore();
            } catch (NullPointerException nullPointerException) {
                tableData[i][6] = "无";
            }

        }
        return studentSCorePane(tableData, name, text);

    }

    public static JScrollPane CourseScoreSetTable() { //课程分数设置表
        String[] name = {"学号", "姓名", "班级", "专业", "教师", "性别", "分数", " "};
        Object[][] tableData = new Object[0][8];
        return jScrollPane(tableData, name);


    }

    /**
     * 封装的比赛信息显示表
     *
     * @param data
     * @return
     */
    public static JScrollPane viewSubjectTable(List<Subject> data) {
        String[] name = {"id", "比赛名称", "获奖级别", "比赛级别", "比赛类别", "人数", "比赛时间", "修改时间"};
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (data == null) {
            return jScrollPane(new Object[0][8], name);
        }
        Object[][] tableData = new Object[data.size()][8];
        for (int i = 0; i < data.size(); i++) {
            tableData[i][0] = data.get(i).getSubjectId();
            tableData[i][1] = data.get(i).getSubjectName();
            tableData[i][2] = data.get(i).getSubjectGrade();
            tableData[i][3] = data.get(i).getSubjectLevel();
            tableData[i][4] = data.get(i).getSubjectCategory();
            tableData[i][5] = data.get(i).getSubjectCount();
            tableData[i][6] = dateFormat.format(data.get(i).getSubjectTime());
            tableData[i][7] = dateFormat.format(data.get(i).getUpdateTime());
        }
        return jScrollPane(tableData, name);
    }

    /**
     * 创建一个特定的Table表
     *
     * @param tableData 表数据
     * @param name      表头数据
     * @return 表
     */
    private static JScrollPane jScrollPane(Object[][] tableData, String[] name) {
        JTable table = new JTable(tableData, name);
        table.setSize(1000, 800);
        table.setBackground(Color.WHITE);
        table.setRowHeight(32);//设置行高
        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        r.setHorizontalAlignment(JLabel.CENTER);
        table.setDefaultRenderer(Object.class, r);
        // 设置表格内容颜色
        table.getColumnModel().getColumn(6).setPreferredWidth(100);
        table.getColumnModel().getColumn(7).setPreferredWidth(100);
        table.setForeground(new Color(47, 53, 66));                   // 字体颜色
        table.setFont(new Font(null, Font.PLAIN, 15));      // 字体样式
        table.setSelectionForeground(new Color(236, 240, 241));      // 选中后字体颜色
        table.setSelectionBackground(new Color(87, 96, 111));     // 选中后字体背景
        table.setGridColor(new Color(87, 96, 111));                     // 网格颜色
        // 设置表头

        Dimension size = table.getTableHeader().getPreferredSize();
        size.height = 40;//设置新的表头高度32
        table.getTableHeader().setPreferredSize(size);
        table.getTableHeader().setFont(new Font(null, Font.BOLD, 15));// 设置表头名称字体样式
        table.getTableHeader().setForeground(Color.white);// 设置表头名称字体颜色
        table.getTableHeader().setResizingAllowed(false);// 设置不允许手动改变列宽
        table.getTableHeader().setReorderingAllowed(false);// 设置不允许拖动重新排序各列
        table.getTableHeader().setBackground(new Color(47, 53, 66));
        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);//居中
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(770, 568));
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.getHSBColor(0.0000F, 0.0000F, 0.9333F)));
        scrollPane.setVisible(true);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    studentCourseInfo.newText = Long.valueOf(table.getValueAt(table.getSelectedRow(), 0).toString());
                    System.out.println(studentCourseInfo.newText);
                }
            }
        });
        return scrollPane;
    }


    private static JScrollPane studentSCorePane(Object[][] tableData, String[] name, Long text) { //学生成绩设置表
        JTable table = new JTable(tableData, name);
        table.setSize(1000, 800);
        table.setBackground(Color.WHITE);
        table.setRowHeight(32);//设置行高
        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        r.setHorizontalAlignment(JLabel.CENTER);
        table.setDefaultRenderer(Object.class, r);
        table.getColumnModel().getColumn(4).setPreferredWidth(40);
        table.getColumnModel().getColumn(5).setPreferredWidth(30);
        table.getColumnModel().getColumn(6).setPreferredWidth(30);
        table.getColumnModel().getColumn(7).setPreferredWidth(100);
        // 设置表格内容颜色
        table.setForeground(new Color(47, 53, 66));                   // 字体颜色
        table.setFont(new Font(null, Font.PLAIN, 15));      // 字体样式
        table.setSelectionForeground(new Color(236, 240, 241));      // 选中后字体颜色
        table.setSelectionBackground(new Color(87, 96, 111));     // 选中后字体背景
        table.setGridColor(new Color(87, 96, 111));                     // 网格颜色
        // 设置表头

        Dimension size = table.getTableHeader().getPreferredSize();
        size.height = 40;//设置新的表头高度32
        table.getTableHeader().setPreferredSize(size);
        table.getTableHeader().setFont(new Font(null, Font.BOLD, 15));// 设置表头名称字体样式
        table.getTableHeader().setForeground(Color.white);// 设置表头名称字体颜色
        table.getTableHeader().setResizingAllowed(false);// 设置不允许手动改变列宽
        table.getTableHeader().setReorderingAllowed(false);// 设置不允许拖动重新排序各列
        table.getTableHeader().setBackground(new Color(47, 53, 66));
        ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);//居中
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(770, 568));
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.getHSBColor(0.0000F, 0.0000F, 0.9333F)));
        scrollPane.setVisible(true);

        table.getColumnModel().getColumn(7).setCellRenderer(new MyButtonRender() {
        });//添加按钮
        table.setRowSelectionAllowed(false);
        //  table.getColumnModel().getColumn(7).setCellEditor(new MyButtonEditor(tableData[table.getSelectedRow()][0]){
        table.getColumnModel().getColumn(7).setCellEditor(new MyButtonEditor(table, text) {
        });//设置按钮
        return scrollPane;
    }


}
